$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'quotePrintFactory/quoteprintfactory/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
			{ label: '公司id', name: 'factoryId', index: 'factory_id', width: 80,hidden:true},
            { label: '公司名称', name: 'suppName', index: 'supp_name', width: 80},
            { label: '工厂印刷机名称', name: 'printFactoryName', index: 'print_factory_name', width: 80 },
			{ label: '印刷机id', name: 'printId', index: 'print_id', width: 80 ,hidden:true,},
            { label: '印刷机打印类型', name: 'printName', index: 'print_Name', width: 80 ,},
			{ label: '最大尺寸(长)', name: 'maxLength', index: 'max_length', width: 80 }, 			
			{ label: '最大尺寸(宽)', name: 'maxWide', index: 'max_wide', width: 80 }, 			
			{ label: '涨价尺寸长', name: 'riseLenght', index: 'rise_lenght', width: 80 }, 			
			{ label: '涨价尺寸宽', name: 'riseWide', index: 'rise_wide', width: 80 }, 			
			{ label: '最小尺寸(长)', name: 'minLength', index: 'min_length', width: 80 }, 			
			{ label: '最小尺寸(宽)', name: 'minWide', index: 'min_wide', width: 80 }, 			
			{ label: '最小克重', name: 'minWeight', index: 'min_weight', width: 80 }, 			
			{ label: '最大克重', name: 'maxWeight', index: 'max_weight', width: 80 }, 			
			{ label: '大数量值', name: 'largeQuantity', index: 'large_quantity', width: 80 }, 			
			{ label: '出血尺寸', name: 'deficit', index: 'deficit', width: 80 },
			{ label: '优先条件', name: 'priority', index: 'priority', width: 80 }, 			
			{ label: '起始数量', name: 'startAmount', index: 'start_amount', width: 80 }, 			
			{ label: '增幅数量', name: 'stepAmount', index: 'step_amount', width: 80 }, 			
			{ label: '特种纸涨价系数', name: 'ratio', index: 'ratio', width: 80 },
			{ label: '印刷机机器类型', name: 'printType', index: 'print_type', width: 80,
                formatter: function (value, options, row) {
                    var valueText = "";
                    var printTypeList = $("#jqGrid").jqGrid('getGridParam', 'userData').quotePrintFactoryEnum;
                    $.each(printTypeList, function (index, module) {
                        if (module.value == value) {
                            valueText = module.describe;
                            return false;
                        }
                    });
                    return valueText;
                }
			},
            { label: '是否在前端显示', name: 'display', index: 'display', width: 80 ,
                formatter: function (value, options, row) {
                    var actions = [];
                    if(value){
                        actions.push('<label class="iSwitch"> <input type="checkbox"  onclick="vm.updatedisplay('+row.id+')" checked> <i></i></label>');
                    }else{
                        actions.push('<label class="iSwitch"> <input type="checkbox" onclick="vm.updatedisplay('+row.id+')"> <i></i></label>');
                    }
                    return actions.join('  ');
                }
            },
            { label: '操作', name: 'option', width: 50 , align: 'center',
                formatter:function(value, options, row){
                    var actions = [];
                    // 配价
                    actions.push('<a href="javascript:void(0);" onclick="return vm.edit(\''+ row.id +'\');">配价</a>') ;
                    return actions.join('  ');
                }
            },
        ],
		viewrecords: true,
        height: window.innerHeight - 120,
        rowNum: 25,
        rowList : [25,50,100],
        rownumbers: true,
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount",
            userdata: "userData"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
            vm.l.quotePrintList = $("#jqGrid").jqGrid('getGridParam', 'userData').quotePrintList;
            vm.e.quotePrintFactoryEnum=$("#jqGrid").jqGrid('getGridParam', 'userData').quotePrintFactoryEnum;
            vm.e.quotePrintPrintTypeEnum=$("#jqGrid").jqGrid('getGridParam', 'userData').quotePrintPrintTypeEnum;
      		vm.e.quotePrintFactoryPriceColourEnum=$("#jqGrid").jqGrid('getGridParam', 'userData').quotePrintFactoryPriceColourEnum;
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
        updateFlag:false,
        quotePrintFactory:{},
		/*初始化包装类*/
        quotePrintFactoryVo:{quotePrintFactory: {canUv:false},quotePrintFacotoryPirceList:[{begin_price:'',step_price:'',display:true}],uVQuotePrintFacotoryPirceList:[]},
        /* 查询 */
        q:{
            printFactoryName: '',
        },
        e: {
            /* 工厂打印机机器类型 */
            quotePrintFactoryEnum:[],
            /* 工厂打印机打印类型 */
            quotePrintPrintTypeEnum:[],
            /* 工厂打印机意颜色类型 */
            quotePrintFactoryPriceColourEnum:[],
        },
        /* 接收查询结果 */
        l: {
            quotePrintList: [],
        },
        //是否按价格系数配置特种纸变价
        followUv:false,
        //特种纸张数据
        uVQuotePrintFacotoryPirceList:[],
	},
    watch:{
	  'quotePrintFactoryVo.quotePrintFacotoryPirceList':{
          handler:function(newValue,oldValue){
              if(!vm.updateFlag){
                  //开启了特种纸价格设置才拷贝
                  if(vm.quotePrintFactoryVo.quotePrintFactory.canUv){
                      vm.copyPrice();
                  }
              }else{
                  vm.copyUpdatePrice();
              }
          },
          deep:true,
      }
    },
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			<!--包装类封装数据-->
            vm.quotePrintFactoryVo = {quotePrintFactory:{},quotePrintFacotoryPirceList:[{colour:'',beginPrice:0,stepPrice:0,largePrice:0,riseBeginPrice:0,riseStepPrice:0,riseLarge:0,printPlatePrice:0,floatingCoefficient:0,display:true}]};
			vm.uVQuotePrintFacotoryPirceList=[];
			vm.showList = false;
            vm.updateFlag = false;
			vm.title = "新增";
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
        edit: function (id) {
            vm.getInfo(id)
        },
        //校验参数
        factoryValidate:function(factory){
            if(isBlank(vm.quotePrintFactoryVo.quotePrintFactory.minWeight)){
                alert("请输入最小克重!");
                return false;
            }
            if(isBlank(vm.quotePrintFactoryVo.quotePrintFactory.maxWeight)){
                alert("请输入最大克重!");
                return false;
            }
            return true;
        },
        saveOrUpdate : function (event) {
		   if(vm.factoryValidate()) {
               //存放特种纸张价格
               if(!vm.quotePrintFactoryVo.quotePrintFactory.canUv){
                   vm.uVQuotePrintFacotoryPirceList=[];
               }
               if(!vm.followUv){
                   vm.quotePrintFactoryVo.quotePrintFactory.ratio=0;
               }
               vm.quotePrintFactoryVo.uVQuotePrintFacotoryPirceList=vm.uVQuotePrintFacotoryPirceList;
               var url = vm.quotePrintFactoryVo.quotePrintFactory.id == null ? "quotePrintFactory/quoteprintfactory/saveWithPrice" : "quotePrintFactory/quoteprintfactory/updateWithPrice";
               $.ajax({
                   type: "POST",
                   url: baseURL + url,
                   contentType: "application/json",
                   data: JSON.stringify(vm.quotePrintFactoryVo),
                   success: function(r){
                       if(r.code === 0){
                           alert('操作成功', function(index){
                               vm.reload();
                           });
                       }else{
                           alert(r.msg);
                       }
                   }
               });
           }
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "quotePrintFactory/quoteprintfactory/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
            window.quotePrintFactoryVm.loadData({
                'id':id,
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{//postData此数组内容直接赋值到url上，参数类型：{name1:value1…}
                    'printFactoryName': vm.q.printFactoryName.trim(),
                },
                page:page
            }).trigger("reloadGrid");
		},
		reset: function (event) {
			vm.q.printFactoryName = '';
			vm.reload();
		},
        deleteRow(index, rows) {//删除改行
            rows.splice(index, 1);
        },
        addRow(tableData,event){
            tableData.push({colour:'',beginPrice:0,stepPrice:0,largePrice:0,riseBeginPrice:0,riseStepPrice:0,riseLarge:0,printPlatePrice:0,floatingCoefficient:0,display:true});
            vm.uVQuotePrintFacotoryPirceList.push({colour:'',beginPrice:0,stepPrice:0,largePrice:0,riseBeginPrice:0,riseStepPrice:0,riseLarge:0,printPlatePrice:0,floatingCoefficient:0,display:true});
        },
        //修改是否显示状态
        updatedisplay(id){
            var url ="quotePrintFactory/quoteprintfactory/updateDisplay"+id;
            $.get(baseURL + "quotePrintFactory/quoteprintfactory/updateDisplay/"+id, function(r){
                if(r.code == 0){
                    alert('操作成功', function(index){
                        $("#jqGrid").trigger("reloadGrid");
                    });
                }else{
                    alert(r.msg);
                }
            });
        },
        //开启特种纸价格设置
        changeUv:function () {
            //如果是新增，则直接将普通纸价格复制下来
            if (vm.quotePrintFactoryVo.quotePrintFactory.canUv) {
                vm.uVQuotePrintFacotoryPirceList = JSON.parse(JSON.stringify(vm.quotePrintFactoryVo.quotePrintFacotoryPirceList))
            }
        },
        copyPrice:function() {
		    //如果没开启特种纸价格设置
		    if(!vm.followUv){
                vm.uVQuotePrintFacotoryPirceList=JSON.parse(JSON.stringify(vm.quotePrintFactoryVo.quotePrintFacotoryPirceList));
            }
            //如果是新增时，先将数据copy，然后计算
            var list = JSON.parse(JSON.stringify(vm.quotePrintFactoryVo.quotePrintFacotoryPirceList));
            if(vm.followUv){
                if(vm.quotePrintFactoryVo.quotePrintFactory.ratio==0||isBlank(vm.quotePrintFactoryVo.quotePrintFactory.ratio)){
                    vm.quotePrintFactoryVo.quotePrintFactory.ratio=1;
                }
                for (var i = 0; i < list.length; i++) {
                    list[i].colour == vm.quotePrintFactoryVo.quotePrintFacotoryPirceList[i].colour;
                    list[i].beginPrice = vm.quotePrintFactoryVo.quotePrintFacotoryPirceList[i].beginPrice * vm.quotePrintFactoryVo.quotePrintFactory.ratio;
                    list[i].stepPrice = vm.quotePrintFactoryVo.quotePrintFacotoryPirceList[i].stepPrice * vm.quotePrintFactoryVo.quotePrintFactory.ratio;
                    list[i].largePrice = vm.quotePrintFactoryVo.quotePrintFacotoryPirceList[i].largePrice * vm.quotePrintFactoryVo.quotePrintFactory.ratio;
                    list[i].riseBeginPrice = vm.quotePrintFactoryVo.quotePrintFacotoryPirceList[i].riseBeginPrice * vm.quotePrintFactoryVo.quotePrintFactory.ratio;
                    list[i].riseStepPrice = vm.quotePrintFactoryVo.quotePrintFacotoryPirceList[i].riseStepPrice * vm.quotePrintFactoryVo.quotePrintFactory.ratio
                    list[i].riseLarge = vm.quotePrintFactoryVo.quotePrintFacotoryPirceList[i].riseLarge * vm.quotePrintFactoryVo.quotePrintFactory.ratio;
                    list[i].printPlatePrice = vm.quotePrintFactoryVo.quotePrintFacotoryPirceList[i].printPlatePrice * vm.quotePrintFactoryVo.quotePrintFactory.ratio
                    list[i].floatingCoefficient = vm.quotePrintFactoryVo.quotePrintFacotoryPirceList[i].floatingCoefficient;
                }
                vm.uVQuotePrintFacotoryPirceList = list;
            }
        } ,
        //是否按照特种纸张变价
        changeFollowUv:function () {
            if(vm.followUv) {
                if(vm.quotePrintFactoryVo.quotePrintFactory.ratio==0||isBlank(vm.quotePrintFactoryVo.quotePrintFactory.ratio)){
                    vm.quotePrintFactoryVo.quotePrintFactory.ratio=1;
                    vm.copyPrice();
                }
            }
        },
        //改变变价系数
        changeRatio:function(){
          vm.copyPrice();
        },
        //
        colorFormat:function (row) {
		    var colour=row.colour;
           for (var i=0;i<vm.e.quotePrintFactoryPriceColourEnum.length;i++){
               if(colour==vm.e.quotePrintFactoryPriceColourEnum[i].value){
                   return vm.e.quotePrintFactoryPriceColourEnum[i].describe;
               }
           }
        }
	}
});